var tool = '<xml id="toolbox" style="display: none">';
var tool2 = '<xml id="toolbox" style="display: none">';
var Logic = '';
Logic += '<block type="controls_if" ></block>';
Logic += '<block type="logic_compare"></block>';
Logic += '<block type="logic_operation"></block>';
Logic += '<block type="logic_negate"></block>';
Logic += '<block type="logic_boolean"></block>';
Logic += '<block type="logic_null" disabled="false"></block>';
Logic += '<block type="logic_ternary"></block>';

var Loops = '';
Loops += '<block type="controls_repeat"></block>';
//Loops += '<block type="controls_for"><value name="FROM"><shadow type="math_number"><field name="NUM">1</field></shadow></value><value name="TO"><shadow type="math_number"><field name="NUM">10</field></shadow></value><value name="BY"><shadow type="math_number"><field name="NUM">1</field></shadow></value></block>';
//Loops += '<block type="controls_flow_statements"></block>';


var BasicLib = ''
BasicLib += '<block type="Goertek_Start" ></block>';
BasicLib += '<block type="Goertek_UnLock"></block>';
BasicLib += '<block type="Goertek_Lock"></block>';
BasicLib += '<block type="Goertek_TakeOff"></block>';
BasicLib += '<block type="Goertek_Land"></block>';
var BasicLib2 = ''
BasicLib2 += '<block type="Goertek_Start" ></block>';
BasicLib2 += '<block type="Goertek_UnLock"></block>';
BasicLib2 += '<block type="Goertek_Lock"></block>';
BasicLib2 += '<block type="Goertek_TakeOff2"></block>';
BasicLib2 += '<block type="Goertek_Land"></block>';

var ActionLib = '';
ActionLib += '<block type="block_inittime"></block>';
ActionLib += '<block type="Goertek_Point"></block>';
ActionLib += '<block type="block_delay" ></block>';
ActionLib += '<block type="Goertek_HorizontalSpeed" ></block>';
ActionLib += '<block type="Goertek_HorizontalAcceleration" ></block>';
ActionLib += '<block type="Goertek_VerticalSpeed" ></block>';
ActionLib += '<block type="Goertek_VerticalAcceleration" ></block>';
ActionLib += '<block type="Goertek_AngularVelocity" ></block>';
ActionLib += '<block type="Goertek_TurnTo" ></block>';
ActionLib += '<block type="Goertek_Turn" ></block>';
ActionLib += '<block type="Goertek_MoveToCoord" ></block>';
ActionLib += '<block type="Goertek_MoveToPoint" ></block>';
ActionLib += '<block type="Goertek_Move" ></block>';
// ActionLib += '<block type="Goertek_ReachPosition" ></block>';
// ActionLib += '<block type="Goertek_ReachPositionDelta" ></block>';
// ActionLib += '<block type="Goertek_ReachToPoint" ></block>';


var ReachLib = ''
ReachLib += '<block type="Goertek_ReachPosition" ></block>';
ReachLib += '<block type="Goertek_ReachPositionDelta" ></block>';
ReachLib += '<block type="Goertek_ReachToPoint" ></block>';
ReachLib += '<block type="Goertek_ReachAngle" ></block>';
ReachLib += '<block type="Goertek_ReachAngleDelta" ></block>';


var advancedActionLib = ''
advancedActionLib += '<block type="Goertek_HighSpeedTranslate" ></block>';
advancedActionLib += '<block type="Goertek_TurnTranslate" ></block>';
advancedActionLib += '<block type="Goertek_SpiralTranslate" ></block>';
advancedActionLib += '<block type="Goertek_SimpleHarmonicMotio" ></block>';
advancedActionLib += '<block type="Goertek_RollInAir" ></block>';

var ledlib = ''
ledlib += '<block type="Goertek_AtomicLEDOn" ></block>';
ledlib += '<block type="Goertek_AtomicLEDOff" ></block>';
ledlib += '<block type="Goertek_LEDSingleBlink" ></block>';
ledlib += '<block type="Goertek_LEDBreath" ></block>';
ledlib += '<block type="Goertek_LEDTurnOffAll" ></block>';
ledlib += '<block type="Goertek_LEDTurnOnAllSingleColor" ></block>';
ledlib += '<block type="Goertek_LEDTurnOnAll" ></block>';
ledlib += '<block type="Goertek_LEDBlinkFastAll" ></block>';
ledlib += '<block type="Goertek_LEDBlinkSlowAll" ></block>';
ledlib += '<block type="Goertek_LEDHorseRacel" ></block>';

var ledlib2 = ''
ledlib2 += '<block type="Goertek_LEDTurnOnAllSingleColor2" ></block>';
ledlib2 += '<block type="Goertek_LEDTurnOffAll2" ></block>';
ledlib2 += '<block type="Goertek_LEDBlinkALL2" ></block>';
ledlib2 += '<block type="Goertek_LEDBreathALL2" ></block>';
ledlib2 += '<block type="Goertek_LEDTurnOnAllSingleColor3" ></block>';
ledlib2 += '<block type="Goertek_LEDTurnOffAll3" ></block>';
ledlib2 += '<block type="Goertek_LEDBlinkALL3" ></block>';
ledlib2 += '<block type="Goertek_LEDBreathALL3" ></block>';
ledlib2 += '<block type="Goertek_LEDTurnOnAllSingleColor4" ></block>';
ledlib2 += '<block type="Goertek_LEDTurnOffAll4" ></block>';
ledlib2 += '<block type="Goertek_LEDBlinkALL4" ></block>';
ledlib2 += '<block type="Goertek_LEDBreathALL4" ></block>';
ledlib2 += '<block type="Goertek_LEDHorseALL4" ></block>';


var buzzerlib = ''
buzzerlib += '<block type="Goertek_BuzzerControl" ></block>';


var buttontree = '';
buttontree += '<button text="基本功能" callbackKey="basic"  web-class="basicStyle"></button>';
buttontree += '<sep gap="5"></sep>';
buttontree += '<button text="基础动作" callbackKey="action"  web-class="actionStyle"></button>';
buttontree += '<sep gap="5"></sep>';
//buttontree += '<button text="Reach控件" callbackKey="Reach"  web-class="ReachStyle"></button>';
//buttontree += '<sep gap="5"></sep>';
//buttontree += '<button text="进阶动作1" callbackKey="advancedAction"  web-class="actionStyle"></button>';
//buttontree += '<sep gap="5"></sep>';
buttontree += '<button text="显示控件" callbackKey="led"  web-class="ledStyle"></button>';
buttontree += '<sep gap="5"></sep>';
//buttontree += '<button text="声音控件" callbackKey="buzzer"  web-class="buzzerStyle"></button>';
//buttontree += '<sep gap="5"></sep>';
buttontree += '<button text="循环控件" callbackKey="loops"  web-class="loopsStyle"></button>';
buttontree += '<sep gap="5"></sep>';
//buttontree += '<button text="自定义" callbackKey="myblock"  web-class="myblockStyle"></button>';
//buttontree += '<sep gap="0"></sep>';
var label1 = '<label  text="———————————" web-class="myLabelStyle"></label>';
var label2 = '<label  text="———————————————————————————" web-class="myLabelStyle"></label>';

buttontree += label1;

var buttontree2 = '';
buttontree2 += '<button text="基本功能" callbackKey="basic2"  web-class="basicStyle"></button>';
buttontree2 += '<sep gap="5"></sep>';
buttontree2 += '<button text="基础动作" callbackKey="action2"  web-class="actionStyle"></button>';
buttontree2 += '<sep gap="5"></sep>';
//buttontree2 += '<button text="Reach控件" callbackKey="Reach"  web-class="ReachStyle"></button>';
//buttontree2 += '<sep gap="5"></sep>';
//buttontree2 += '<button text="进阶动作" callbackKey="advancedAction2"  web-class="actionStyle"></button>';
//buttontree2 += '<sep gap="5"></sep>';
buttontree2 += '<button text="显示控件" callbackKey="led2"  web-class="ledStyle"></button>';
buttontree2 += '<sep gap="5"></sep>';
//buttontree2 += '<button text="声音控件" callbackKey="buzzer"  web-class="buzzerStyle"></button>';
//buttontree2 += '<sep gap="5"></sep>';
buttontree2 += '<button text="循环控件" callbackKey="loops2"  web-class="loopsStyle"></button>';
buttontree2 += '<sep gap="5"></sep>';
//buttontree2 += '<button text="自定义" callbackKey="myblock"  web-class="myblockStyle"></button>';
//buttontree2 += '<sep gap="0"></sep>';
var label1 = '<label  text="———————————" web-class="myLabelStyle"></label>';
var label2 = '<label  text="———————————————————————————" web-class="myLabelStyle"></label>';

buttontree2 += label1;


var myblocks = '';
var colorLib11 = '<category name="Start at:" custom="COLOUR_PALETTE"></category>';

var workspace;
var colorLib = '<button text="Start at:" callbackKey="CREATE_VARIABLE"></button>';
colorLib += '<block type="block_delay"></block>';
colorLib += '<block type="Goertek_Start"></block>';

tool = tool + buttontree + '</xml>';

tool2 = tool2 + buttontree2 + '</xml>';

//tool = tool+Logic+Loops+BasicLib+ActionLib+'</xml>';

blockinit();
/**/ //调试时候用


// var aa = [];
// var addBasicLib = ['Goertek_UnLock', 'Goertek_Lock', 'Goertek_AbsDistance', 'Goertek_AbsRotation'];
// var addBasicLib22 = ["Goertek_UnLock", "Goertek_AbsDistance", "Goertek_AbsRotation"];
// var cmdq = "Goertek_UnLock,Goertek_Lock,Goertek_AbsDistance";
window.onload = function() //用window的onload事件，窗体加载完毕的时候
    { //
        // flyoutCategory();
        //  blockinit();

        //  blockinit();
        //  workspace.addChangeListener(myUpdateFunction);
        // getchange();
        //   blockchange(cmdq);
        registerbutton();
    }
    //注册按钮

var hasFocusInittimeBlock;

function registerbutton() {
    workspace.registerButtonCallback('basic', basic);
    workspace.registerButtonCallback('action', action);
	workspace.registerButtonCallback('Reach', Reach);
    workspace.registerButtonCallback('advancedAction', advancedAction);
    workspace.registerButtonCallback('logic', logic);
    workspace.registerButtonCallback('loops', loops);
    workspace.registerButtonCallback('basic2', basic2);
    workspace.registerButtonCallback('action2', action2);
    workspace.registerButtonCallback('advancedAction2', advancedAction2);
    workspace.registerButtonCallback('logic2', logic2);
    workspace.registerButtonCallback('loops2', loops2);
    workspace.registerButtonCallback('led', led);
    workspace.registerButtonCallback('led2', led2);
    workspace.registerButtonCallback('buzzer', buzzer);
    workspace.registerButtonCallback('myblock', myblock);

    workspace.updateToolbox(tool);
    workspace.addChangeListener(function(event) {
        // console.log(event);
        if (event instanceof Blockly.Events.Ui) {
            if ((event.newValue != null && typeof(event.blockId) == "undefined") || (event.blockId != null && event.newValue == null)) {
                var bid;
                if (event.newValue != null) {
                    bid = event.newValue;
                } else {
                    bid = event.blockId;
                }
                var block = workspace.getBlockById(bid);
                if (block.type == "block_inittime") {
                    hasFocusInittimeBlock = block;
                    if (event.newValue != null && event.oldValue != null && event.newValue == event.oldValue) {
                        // alert("创建");
                        updateTime(starttime);
                    }
                } else {
                    hasFocusInittimeBlock = null;
                }

            } else {
                hasFocusInittimeBlock = null;
            }
        }
    });
}

var starttime = "00:00";

function updateTime(time) {
    starttime = time;
    if (hasFocusInittimeBlock != null) {
        hasFocusInittimeBlock.setFieldValue(starttime, 'time');
        // alert(hasFocusInittimeBlock.getFieldValue('time'));
    }
}
//无用
function insert_flg(str, flg, sn) {
    var newstr = "";
    for (var i = 0; i < str.length; i += sn) {
        var tmp = str.substring(i, i + sn);
        newstr += tmp + flg;
    }
    return newstr;
}
var lasttool = '';

function importMyblock(myblocks) {
    var index = myblocks.indexOf('<block');
    var lastblock = myblocks.substring(index);
    lastblock = lastblock.replace('</xml>', '');
    lasttool = lasttool + lastblock;
}
//保存自定义
function savemyblock(type) {
    console.log("type="+type);
	SetFlightDeviceType(type);
    myblocks = BlocksToXml();
    var index = myblocks.indexOf('<block');
    var lastblock = myblocks.substring(index);
    lastblock = lastblock.replace('</xml>', '');
    lasttool = lasttool + lastblock;
}

function myblock() {
    tool = '<xml id="toolbox" style="display: none">';
    tool = tool + buttontree + lasttool + '</xml>';
    workspace.updateToolbox(tool);
}

function action() {
    tool = '<xml id="toolbox" style="display: none">';
    tool = tool + buttontree + ActionLib + '</xml>';
    workspace.updateToolbox(tool);
}

function advancedAction() {
    tool = '<xml id="toolbox" style="display: none">';
    tool = tool + buttontree + advancedActionLib + '</xml>';
    workspace.updateToolbox(tool);
}
function Reach() {
    tool = '<xml id="toolbox" style="display: none">';
    tool = tool + buttontree + ReachLib + '</xml>';
    workspace.updateToolbox(tool);
}

function basic() {
    tool = '<xml id="toolbox" style="display: none">';
    tool = tool + buttontree + BasicLib + '</xml>';
    //  workspace.registerButtonCallback('basic', basic);
    workspace.updateToolbox(tool);
}

function logic() {
    tool = '<xml id="toolbox" style="display: none">';
    tool = tool + buttontree + Logic + '</xml>';
    // workspace.registerButtonCallback('logic', logic);
    workspace.updateToolbox(tool);
}

function loops() {
    tool = '<xml id="toolbox" style="display: none">';
    tool = tool + buttontree + Loops + '</xml>';
    //  workspace.registerButtonCallback('loops', loops);
    workspace.updateToolbox(tool);
}

function led() {
    tool = '<xml id="toolbox" style="display: none">';
    tool = tool + buttontree + ledlib + '</xml>';
    //  workspace.registerButtonCallback('loops', loops);
    workspace.updateToolbox(tool);
}
 
function action2() {
    tool2 = '<xml id="toolbox" style="display: none">';
    tool2 = tool2 + buttontree2 + ActionLib + '</xml>';
    workspace.updateToolbox(tool2);
}

function advancedAction2() {
    tool2 = '<xml id="toolbox" style="display: none">';
    tool2 = tool2 + buttontree2 + advancedActionLib + '</xml>';
    workspace.updateToolbox(tool2);
}
 

function basic2() {
    tool2 = '<xml id="toolbox" style="display: none">';
    tool2 = tool2 + buttontree2 + BasicLib2 + '</xml>';
    //  workspace.registerButtonCallback('basic', basic);
    workspace.updateToolbox(tool2);
}

function logic2() {
    tool2 = '<xml id="toolbox" style="display: none">';
    tool2 = tool2 + buttontree2 + Logic + '</xml>';
    // workspace.registerButtonCallback('logic', logic);
    workspace.updateToolbox(tool2);
}

function loops2() {
    tool2 = '<xml id="toolbox" style="display: none">';
    tool2 = tool2 + buttontree2 + Loops + '</xml>';
    //  workspace.registerButtonCallback('loops', loops);
    workspace.updateToolbox(tool2);
}
function led2() {
    tool2 = '<xml id="toolbox" style="display: none">';
    tool2 = tool2 + buttontree2 + ledlib2 + '</xml>';
    //  workspace.registerButtonCallback('loops', loops);
    workspace.updateToolbox(tool2);
}

function buzzer() {
    tool = '<xml id="toolbox" style="display: none">';
    tool = tool + buttontree + buzzerlib + '</xml>';
    //  workspace.registerButtonCallback('loops', loops);
    workspace.updateToolbox(tool);
}

function blockinit() {

    workspace = Blockly.inject('blocklyDiv', {
        media: '../../statics/plugins/blockly/blockly-master/media/',
        toolbox: tool,
        scrollbars: true,
        trashcan: true,
        comments: true,
        grid: {
            spacing: 0,
            length: 3,
            colour: '#ccc',
            snap: true
        },
        zoom: {
            controls: true,
            wheel: true,
            startScale: 1.0,
            maxScale: 3,
            minScale: 0.3,
            scaleSpeed: 1.2
        }
    });
}
//自己调试用
// function getchange() {
//     blockchange(cmdq);
// }
//ada传进来显示动作库函数
function blockchange(blocklychange) {
    var length = blocklychange.length;
    if (length == 0) {
        tool = tool + Logic + Loops + BasicLib + '</xml>';
    } else {

        var sArray = blocklychange.split(",");
        var sArraylength = sArray.length;
        for (var i = 0; i < sArraylength; i++) {
            ActionLib += '<block type="' + sArray[i] + '"></block>';
        }
    }
}

//workspace.registerToolboxCategoryCallback('COLOUR_PALETTE', coloursFlyoutCallback);